package com.magicborrow.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.adapter.CollectionAdapter;
import com.magicborrow.beans.ConllectionListBean;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.utils.UIHelper;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.RefreshRecyclerView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/17.
 */
public class CollectionsActivity extends BaseActivity implements View.OnClickListener,VolleyTool.HTTPListener{

    private RefreshRecyclerView mRefreshRecyclerView;
    private GridLayoutManager mGridLayoutManager;
    private ArrayList<ConllectionListBean.DataEntity.ContentEntity> mData;
    private CollectionAdapter mCollectionAdapter;
    private ImageView iv_back;
    private int page = 0;
    private int size = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collections);
        initView();
        initListener();
        initData();
        sendRequest();
    }

    private void initView(){
        mRefreshRecyclerView = (RefreshRecyclerView)findViewById(R.id.refreshRecyclerView);
        mGridLayoutManager = new GridLayoutManager(this,2);
        mRefreshRecyclerView.setLayoutManager(mGridLayoutManager);
        iv_back = (ImageView) findViewById(R.id.iv_back);
    }

    private void initData(){
        mData = new ArrayList<>();
        mCollectionAdapter = new CollectionAdapter(mData);
        mCollectionAdapter.setWidth(UIHelper.getScreenWidth(this)/2);
        mRefreshRecyclerView.setAdapter(mCollectionAdapter);
    }

    private void initListener(){
        mRefreshRecyclerView.setOnRefreshListener(new RefreshRecyclerView.OnRefreshListener() {
            @Override
            public void onRefreshListener() {
                page = 0;
                sendRequest();
            }
        });
        mRefreshRecyclerView.setOnPullListener(new RefreshRecyclerView.onPullListener() {
            @Override
            public void onPullListener() {
                page++;
                sendRequest();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void sendRequest(){
        Map<String,String> params = new HashMap<>();
        params.put("page",page+"");
        params.put("size",size+"");
        params.put("acc_token", UserTools.getUser(this).getAcc_token());
        VolleyTool.get(Constants.GET_FAVERITE_URL,params,this,0, ConllectionListBean.class);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        ConllectionListBean bean = (ConllectionListBean) t;
        if (bean.getCode() == 0) {
            mRefreshRecyclerView.setLoadFinish();
            if (page == 0) {
                mCollectionAdapter.getData().clear();
                mData.addAll(bean.getData().getContent());
                mCollectionAdapter.notifyDataSetChanged();
            } else {
                if (bean.getData().getContent().size() > 0) {
                    int startPosition = mCollectionAdapter.getItemCount();
                    mCollectionAdapter.getData().addAll(bean.getData().getContent());
                    mCollectionAdapter.notifyItemRangeInserted(startPosition, bean.getData().getContent().size());
                } else {
                    mRefreshRecyclerView.setIsLastPage(true);
                    mRefreshRecyclerView.setLoadFinish();
                }
            }
        } else {
            showShortMsg(bean.getMessage());
        }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {
        showShortMsg("服务器暂时无法连接");
    }
}
